import numpy as np
import platgo as pg


class Ackley(pg.Problem):
    
    def __init__(self, D: int = 2) -> None:
        self.name = 'Ackley'
        self.type = '111'
        self.M = 1
        self.D = D
        lb = [-32] * D
        ub = [32] * D
        self.borders = np.array([lb, ub])
        super().__init__()

    def cal_obj(self, pop: pg.Population) -> None:
        x = pop.decs
        n = self.D
        pop.objv = np.array([-20 * np.exp(-0.2 * np.sqrt(1 / n * np.sum(x ** 2, 1))) -
                                    np.exp(1 / n * np.sum(np.cos(2 * np.pi * x), 1)) + np.e + 20]).T
    
    def get_optimal(self) -> np.ndarray:
        return np.array([[0]])
